package random.fox.p2p.interactors;

import android.util.Log;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import random.fox.p2p.App;
import random.fox.p2p.api.ApiClient;
import random.fox.p2p.bean.BeanModule;
import random.fox.p2p.bean.greendao.Message;
import random.fox.p2p.bean.greendao.User;

/**
 * Created by delong on 2015/3/24.
 */
public class MessageInteractorImpl implements MessageInteractor {

    public String TAG = "MessageInteractorImpl";
    @Inject
    User user;
    private App app;
    ApiClient apiClient;
    List<Message> hotMessages;
    List<Message> likeMessages;

    public MessageInteractorImpl(App app) {
        this.app = app;
        apiClient = app.getApiClient();
        app.createScopedGraph(new BeanModule()).inject(this);
    }

    @Override
    public void addMessage(String theme, String content, File image, final OnSendMessageListener listener) {
        Map<String, String> params = new HashMap<>();
//        params.put("uid", user.getUid());
        params.put("uid", user.getUid());
        // if (!TextUtils.isEmpty(theme))
        params.put("theme", theme);
        //if (!TextUtils.isEmpty(content))
        params.put("content", content);
        //if (image!=null && image.exists()){
        params.put("img", image.getName());
        //}

        apiClient.sendMessage(params, image, new ApiClient.CallBack<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                listener.onSuccess();
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                //TODO 错误代码的定义还未开始
                listener.onError();//暂时用1
            }
        });
    }

    @Override
    public void addLike( int mid, final OnAddActionListener likeListener) {

        try {
            apiClient.addLike(user.getUid(), mid, new ApiClient.CallBack<JSONObject>() {
                @Override
                public void onResponse(JSONObject o) {
                    likeListener.onSuccess();
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    //TODO 错误代码的定义还未开始
                    likeListener.onError(error);//暂时用1
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            likeListener.onError(e);
        }
    }

    @Override
    public void addReply(int mid, String content, final OnAddActionListener listener) {
        try {
            apiClient.addReply(user.getUid(), mid , content, new ApiClient.CallBack<JSONObject>() {
                @Override
                public void onResponse(JSONObject o) {
                    listener.onSuccess();
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    //TODO 错误代码的定义还未开始
                    listener.onError(error);//暂时用1
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            listener.onError(e);
        }
    }

    @Override
    public void loadMessage(final OnLoadMessageListener listener) {
        try {
            apiClient.todayHot(user.getUid(), new ApiClient.CallBack<JSONArray>() {
                @Override
                public void onResponse(JSONArray o) {
                    Log.d("AAA", "todayHot:" + o);
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<Message>>() {
                    }.getType();
                    hotMessages = gson.fromJson(o.toString(), type);
                    listener.onSuccess(hotMessages);
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    listener.onError(error);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            listener.onError(e);
        }
    }

    @Override
    public void loadLikeMessage(final OnLoadMessageListener listener) {
        try {
            apiClient.getLikeList(user.getUid(), new ApiClient.CallBack<JSONArray>() {
                @Override
                public void onResponse(JSONArray o) {
                    Log.d("AAA", "like:" + o);
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<Message>>() {
                    }.getType();
                    likeMessages = gson.fromJson(o.toString(), type);
                    listener.onSuccess(likeMessages);
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    listener.onError(error);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            listener.onError(e);
        }
    }

    @Override
    public void releaseHotData() {
        if (hotMessages != null) {
            hotMessages.clear();
            hotMessages = null;
        }
    }

    @Override
    public void releaseLikeData() {
        if (likeMessages != null) {
            likeMessages.clear();
            likeMessages = null;
        }
    }


    @Override
    public List<Message> getCacheData() {
        return hotMessages;
    }

    @Override
    public List<Message> getLikeCacheData() {
        return likeMessages;
    }

}
